package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Address;
import com.itheima.reggie.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class AddressController {

    @Autowired
    private AddressService addressService;

    //查出默认地址
    @GetMapping("/address/default")
    public ResultInfo findAddressDefault(){
        Address address = addressService.findAddressDefault();
        return ResultInfo.success(address);
    }

    //查询地址列表
    @GetMapping("/address/list")
    public ResultInfo findAddressAll(){
       List<Address> addressList =  addressService.findAll();
       return ResultInfo.success(addressList);
    }

    //新增地址
    @PostMapping("/address")
    public ResultInfo add(@RequestBody Address address){
        addressService.add(address);
        return ResultInfo.success(null);
    }

    //设置默认地址
    @PutMapping("/address/default")
    public ResultInfo setUpAddress(@RequestBody Map<String,Long> map){
        Long id = map.get("id");
        addressService.setUpAddress(id);
        return ResultInfo.success(null);
    }

    //根据id查询地址
    @GetMapping("/address/{id}")
    public ResultInfo updateAddress(@PathVariable Long id){
        Address address = addressService.findById(id);
        return ResultInfo.success(address);
    }

    //更新地址
    @PutMapping("/address")
    public ResultInfo update(@RequestBody Address address){
        addressService.update(address);
        return ResultInfo.success(null);
    }

    //删除地址（包括批量）
    @DeleteMapping("/address")
    public ResultInfo deleteAddress(@RequestParam List<Long> ids){
        addressService.delete(ids);
        return ResultInfo.success(null);
    }
}
